<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Nonterminal</title>
<link rel="stylesheet" href="../../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../../index.html" title="Spirit 2.59">
<link rel="up" href="../parser_concepts.html" title="Parser Concepts">
<link rel="prev" href="naryparser.html" title="NaryParser">
<link rel="next" href="../basics.html" title="Parser Basics">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="naryparser.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../parser_concepts.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../basics.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h5 class="title">
<a name="spirit.qi.reference.parser_concepts.nonterminal"></a><a class="link" href="nonterminal.html" title="Nonterminal">Nonterminal</a>
</h5></div></div></div>
<h6>
<a name="spirit.qi.reference.parser_concepts.nonterminal.h0"></a>
            <span class="phrase"><a name="spirit.qi.reference.parser_concepts.nonterminal.description"></a></span><a class="link" href="nonterminal.html#spirit.qi.reference.parser_concepts.nonterminal.description">Description</a>
          </h6>
<p>
            A Nonterminal is a symbol in a <a class="link" href="../../../abstracts/parsing_expression_grammar.html" title="Parsing Expression Grammar">Parsing
            Expression Grammar</a> production that represents a grammar fragment.
            Nonterminals may self reference to specify recursion. This is one of
            the most important concepts and the reason behind the word "recursive"
            in recursive descent parsing.
          </p>
<h6>
<a name="spirit.qi.reference.parser_concepts.nonterminal.h1"></a>
            <span class="phrase"><a name="spirit.qi.reference.parser_concepts.nonterminal.refinement_of"></a></span><a class="link" href="nonterminal.html#spirit.qi.reference.parser_concepts.nonterminal.refinement_of">Refinement
            of</a>
          </h6>
<div class="blockquote"><blockquote class="blockquote"><p>
              <a class="link" href="parser.html" title="Parser"><code class="computeroutput"><span class="identifier">Parser</span></code></a>
            </p></blockquote></div>
<h6>
<a name="spirit.qi.reference.parser_concepts.nonterminal.h2"></a>
            <span class="phrase"><a name="spirit.qi.reference.parser_concepts.nonterminal.signature"></a></span><a class="link" href="nonterminal.html#spirit.qi.reference.parser_concepts.nonterminal.signature">Signature</a>
          </h6>
<p>
            Nonterminals can have both synthesized and inherited attributes. The
            Nonterminal's <span class="emphasis"><em>Signature</em></span> specifies both the synthesized
            and inherited attributes. The specification uses the function declarator
            syntax:
          </p>
<pre class="programlisting"><span class="identifier">RT</span><span class="special">(</span><span class="identifier">A0</span><span class="special">,</span> <span class="identifier">A1</span><span class="special">,</span> <span class="identifier">A2</span><span class="special">,</span> <span class="special">...,</span> <span class="identifier">AN</span><span class="special">)</span>
</pre>
<p>
            where <code class="computeroutput"><span class="identifier">RT</span></code> is the Nonterminal's
            synthesized attribute and <code class="computeroutput"><span class="identifier">A0</span></code>
            ... <code class="computeroutput"><span class="identifier">AN</span></code> are the Nonterminal's
            inherited attributes.
          </p>
<p>
            The default value is <code class="computeroutput"><span class="keyword">void</span><span class="special">()</span></code> (no synthesized and inherited attributes).
          </p>
<h6>
<a name="spirit.qi.reference.parser_concepts.nonterminal.h3"></a>
            <span class="phrase"><a name="spirit.qi.reference.parser_concepts.nonterminal.attributes"></a></span><a class="link" href="nonterminal.html#spirit.qi.reference.parser_concepts.nonterminal.attributes">Attributes</a>
          </h6>
<p>
            The Nonterminal models a C++ function. The Nonterminal's synthesized
            attribute is analogous to the function return value and its inherited
            attributes are analogous to function arguments. The inherited attributes
            (arguments) can be passed in just like any <a class="link" href="../basics.html#spirit.qi.reference.basics.lazy_argument">Lazy
            Argument</a>, e.g.:
          </p>
<pre class="programlisting"><span class="identifier">r</span><span class="special">(</span><span class="identifier">expr</span><span class="special">)</span> <span class="comment">// Evaluate expr at parse time and pass the result to the Nonterminal r</span>
</pre>
<h6>
<a name="spirit.qi.reference.parser_concepts.nonterminal.h4"></a>
            <span class="phrase"><a name="spirit.qi.reference.parser_concepts.nonterminal._code__phrase_role__identifier___val__phrase___code_"></a></span><a class="link" href="nonterminal.html#spirit.qi.reference.parser_concepts.nonterminal._code__phrase_role__identifier___val__phrase___code_"><code class="computeroutput"><span class="identifier">_val</span></code></a>
          </h6>
<p>
            The <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">qi</span><span class="special">::</span><span class="identifier">_val</span></code> placeholder can be used in <a href="../../../../../../../../libs/phoenix/doc/html/index.html" target="_top">Boost.Phoenix</a> semantic
            actions anywhere in the Nonterminal's definition. This <a href="../../../../../../../../libs/phoenix/doc/html/index.html" target="_top">Boost.Phoenix</a>
            placeholder refers to the Nonterminal's (synthesized) attribute. The
            <code class="computeroutput"><span class="identifier">_val</span></code> placeholder acts
            like a mutable reference to the Nonterminal's attribute.
          </p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
              Starting with <a href="http://boost-spirit.com" target="_top">Spirit</a> V2.5
              (distributed with Boost V1.47) the placeholder <code class="computeroutput"><span class="identifier">_val</span></code>
              can be used in semantic actions attached to top level parser components
              as well. See <a class="link" href="../parse_api.html" title="Parser API">The Parse
              API</a> for more information.
            </p></td></tr>
</table></div>
<h6>
<a name="spirit.qi.reference.parser_concepts.nonterminal.h5"></a>
            <span class="phrase"><a name="spirit.qi.reference.parser_concepts.nonterminal._code__phrase_role__identifier___r1__phrase___code_______code__phrase_role__identifier__r10__phrase___code_"></a></span><a class="link" href="nonterminal.html#spirit.qi.reference.parser_concepts.nonterminal._code__phrase_role__identifier___r1__phrase___code_______code__phrase_role__identifier__r10__phrase___code_"><code class="computeroutput"><span class="identifier">_r1</span></code> ... <code class="computeroutput"><span class="identifier">r10</span></code></a>
          </h6>
<p>
            The <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">_r1</span></code> ... <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">r10</span></code>
            placeholders can be used in <a href="../../../../../../../../libs/phoenix/doc/html/index.html" target="_top">Boost.Phoenix</a>
            semantic actions anywhere in the Nonterminal's definition. These <a href="../../../../../../../../libs/phoenix/doc/html/index.html" target="_top">Boost.Phoenix</a> placeholders
            refer to the Nonterminal's inherited attributes.
          </p>
<h6>
<a name="spirit.qi.reference.parser_concepts.nonterminal.h6"></a>
            <span class="phrase"><a name="spirit.qi.reference.parser_concepts.nonterminal.locals"></a></span><a class="link" href="nonterminal.html#spirit.qi.reference.parser_concepts.nonterminal.locals">Locals</a>
          </h6>
<p>
            Nonterminals can have local variables that will be created on the stack
            at parse time. A locals descriptor added to the Nonterminal declaration
            will give the Nonterminal local variables:
          </p>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T0</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">T1</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">T2</span><span class="special">,</span> <span class="special">...,</span> <span class="keyword">typename</span> <span class="identifier">TN</span><span class="special">&gt;</span>
<span class="keyword">struct</span> <span class="identifier">locals</span><span class="special">;</span>
</pre>
<p>
            where <code class="computeroutput"><span class="identifier">T0</span></code> ... <code class="computeroutput"><span class="identifier">TN</span></code> are the types of local variables
            accessible in your <a href="../../../../../../../../libs/phoenix/doc/html/index.html" target="_top">Boost.Phoenix</a>
            semantic actions using the placeholders:
          </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
                <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">_a</span></code>
              </li>
<li class="listitem">
                <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">_b</span></code>
              </li>
<li class="listitem">
                <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">_c</span></code>
              </li>
<li class="listitem">
                <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">_d</span></code>
              </li>
<li class="listitem">
                <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">_e</span></code>
              </li>
<li class="listitem">
                <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">_f</span></code>
              </li>
<li class="listitem">
                <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">_g</span></code>
              </li>
<li class="listitem">
                <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">_h</span></code>
              </li>
<li class="listitem">
                <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">_i</span></code>
              </li>
<li class="listitem">
                <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">_j</span></code>
              </li>
</ul></div>
<p>
            which correspond to the Nonterminal's local variables <code class="computeroutput"><span class="identifier">T0</span></code>
            ... <code class="computeroutput"><span class="identifier">T9</span></code>.
          </p>
<div class="variablelist">
<p class="title"><b>Notation</b></p>
<dl class="variablelist">
<dt><span class="term"><code class="computeroutput"><span class="identifier">x</span></code></span></dt>
<dd><p>
                  A Nonterminal
                </p></dd>
<dt><span class="term"><code class="computeroutput"><span class="identifier">X</span></code></span></dt>
<dd><p>
                  A Nonterminal type
                </p></dd>
<dt><span class="term"><code class="computeroutput"><span class="identifier">arg1</span></code>, <code class="computeroutput"><span class="identifier">arg2</span></code>, ..., <code class="computeroutput"><span class="identifier">argN</span></code></span></dt>
<dd><p>
                  <a class="link" href="../basics.html#spirit.qi.reference.basics.lazy_argument">Lazy Arguments</a>
                  that evaluate to each of the Nonterminal's inherited attributes.
                </p></dd>
</dl>
</div>
<h6>
<a name="spirit.qi.reference.parser_concepts.nonterminal.h7"></a>
            <span class="phrase"><a name="spirit.qi.reference.parser_concepts.nonterminal.valid_expressions"></a></span><a class="link" href="nonterminal.html#spirit.qi.reference.parser_concepts.nonterminal.valid_expressions">Valid
            Expressions</a>
          </h6>
<p>
            In addition to the requirements defined in <a class="link" href="parser.html" title="Parser"><code class="computeroutput"><span class="identifier">Parser</span></code></a>, for any Nonterminal
            the following must be met:
          </p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                    <p>
                      Expression
                    </p>
                  </th>
<th>
                    <p>
                      Semantics
                    </p>
                  </th>
<th>
                    <p>
                      Return type
                    </p>
                  </th>
</tr></thead>
<tbody>
<tr>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">x</span></code>
                    </p>
                  </td>
<td>
                    <p>
                      In a parser expression, invoke Nonterminal <code class="computeroutput"><span class="identifier">x</span></code>
                    </p>
                  </td>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">X</span></code>
                    </p>
                  </td>
</tr>
<tr>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">x</span><span class="special">(</span><span class="identifier">arg1</span><span class="special">,</span>
                      <span class="identifier">arg2</span><span class="special">,</span>
                      <span class="special">...,</span> <span class="identifier">argN</span><span class="special">)</span></code>
                    </p>
                  </td>
<td>
                    <p>
                      In a parser expression, invoke Nonterminal <code class="computeroutput"><span class="identifier">r</span></code>
                      passing in inherited attributes <code class="computeroutput"><span class="identifier">arg1</span></code>
                      ... <code class="computeroutput"><span class="identifier">argN</span></code>
                    </p>
                  </td>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">X</span></code>
                    </p>
                  </td>
</tr>
<tr>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">x</span><span class="special">.</span><span class="identifier">name</span><span class="special">(</span><span class="identifier">name</span><span class="special">)</span></code>
                    </p>
                  </td>
<td>
                    <p>
                      Naming a Nonterminal.
                    </p>
                  </td>
<td>
                    <p>
                      <code class="computeroutput"><span class="keyword">void</span></code>
                    </p>
                  </td>
</tr>
<tr>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">x</span><span class="special">.</span><span class="identifier">name</span><span class="special">()</span></code>
                    </p>
                  </td>
<td>
                    <p>
                      Getting the name of a Nonterminal.
                    </p>
                  </td>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span></code>
                    </p>
                  </td>
</tr>
<tr>
<td>
                    <p>
                      debug(x)
                    </p>
                  </td>
<td>
                    <p>
                      Debug Nonterminal <code class="computeroutput"><span class="identifier">x</span></code>.
                    </p>
                  </td>
<td>
                    <p>
                      <code class="computeroutput"><span class="keyword">void</span></code>
                    </p>
                  </td>
</tr>
</tbody>
</table></div>
<h6>
<a name="spirit.qi.reference.parser_concepts.nonterminal.h8"></a>
            <span class="phrase"><a name="spirit.qi.reference.parser_concepts.nonterminal.type_expressions"></a></span><a class="link" href="nonterminal.html#spirit.qi.reference.parser_concepts.nonterminal.type_expressions">Type
            Expressions</a>
          </h6>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                    <p>
                      Expression
                    </p>
                  </th>
<th>
                    <p>
                      Description
                    </p>
                  </th>
</tr></thead>
<tbody>
<tr>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">X</span><span class="special">::</span><span class="identifier">sig_type</span></code>
                    </p>
                  </td>
<td>
                    <p>
                      The Signature of <code class="computeroutput"><span class="identifier">X</span></code>:
                      In a function signature form as described above in the Signature
                      paragraph.
                    </p>
                  </td>
</tr>
<tr>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">X</span><span class="special">::</span><span class="identifier">locals_type</span></code>
                    </p>
                  </td>
<td>
                    <p>
                      The local variables of <code class="computeroutput"><span class="identifier">X</span></code>:
                      An <a href="../../../../../../../../libs/mpl/doc/refmanual/forward-sequence.html" target="_top">MPL
                      Forward Sequence</a>.
                    </p>
                  </td>
</tr>
</tbody>
</table></div>
<h6>
<a name="spirit.qi.reference.parser_concepts.nonterminal.h9"></a>
            <span class="phrase"><a name="spirit.qi.reference.parser_concepts.nonterminal.models"></a></span><a class="link" href="nonterminal.html#spirit.qi.reference.parser_concepts.nonterminal.models">Models</a>
          </h6>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
                <a class="link" href="../nonterminal/rule.html" title="Parser Rule">rule</a>
              </li>
<li class="listitem">
                <a class="link" href="../nonterminal/grammar.html" title="Parser Grammar">grammar</a>
              </li>
</ul></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2001-2011 Joel de Guzman, Hartmut Kaiser<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="naryparser.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../parser_concepts.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../basics.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
